
import Config from "../../common/config.js";
Page({
  /**
   * 页面的初始数据
   */
  data: {
      bookList:{},
    avatarUrl:"",
    nickName:"",
    infoState:true
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.login({
      success: function (res) {
        console.log(res.code)
        //发送请求
        wx.request({
          url: 'https://zhangyushu.liangziqishi.shop/api/app/login', //接口地址
          data: { code: res.code },
          method: 'POST',
          header: {
            'content-type': 'application/json' //默认值
          },
          success: function (res) {
            console.log(res.data)
            wx.setStorageSync("session_key", res.data.data.data.session_key);
            // wx.setStorageSync("sessionId",res.data.data.data.sessionId);
            wx.setStorageSync("openid", res.data.data.data.openid)
            wx.setStorage({
              key: 'sessionId',
              data: 'JSESSIONID=' + res.data.data.data.sessionId,
            })
            wx.getSetting({
              success(res) {
                if (!res.authSetting['scope.userInfo']) {
                    that.setData({
                      infoState:false
                    })
                }
              }
            })
            var sessionId = 'JSESSIONID=' + res.data.data.data.sessionId
            wx.request({
              url: Config.url + '/api/bookshelf/getDataList',
              method: 'POST',
              header: {
                'content-type': 'application/json',
                'cookie': sessionId
              },
              success(res) {
                console.log(res)
                that.setData({
                  bookList: res.data.data.data,
                })

              }
            })

          }

        })
      }
    })
   
  
  },
  
  onGotUserInfo(e) {
    var that = this 
    that.setData({
      infoState: true
    })
    var sessionId = wx.getStorageSync('sessionId')
    wx.request({
      url: Config.url + '/api/user/updateData',
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'cookie': sessionId
      },
      data:{
        icon: e.detail.userInfo.avatarUrl,
        nickName: e.detail.userInfo.nickName
      },
      success(res) {
        console.log(res)
      

      }
    })
    console.log(e.detail.errMsg)
    console.log(e.detail.userInfo)
    console.log(e.detail.rawData)
  },
  goMakebook: function () {
    wx.switchTab({
      url: '../bookTemplate/bookTemplate'
    })
  },
  goPrev: function(e) {
    wx.navigateTo({
      url: '../previe/previe?id='+e.currentTarget.id
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})